package com.gaoyi.basemodule.basedialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewbinding.ViewBinding;

import com.gaoyi.basemodule.R;

public abstract class BaseFragmentDialog<V extends ViewBinding> extends DialogFragment{
    protected static final String CENTER = "center";
    protected static final String BOTTOM = "bottom";
    protected V mBinding;
    protected Context mContext;
    private Dialog mDialog;
    private boolean mCancel;

    private DialogInterface.OnKeyListener mOnKeyListener;

    public BaseFragmentDialog() {
    }

    public BaseFragmentDialog(Context context) {
        this.mContext = context;
    }

    public BaseFragmentDialog(Context context,DialogInterface.OnKeyListener onKeyListener) {
        this.mContext = context;
        this.mOnKeyListener = onKeyListener;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int dialogStyle = getShowTag().equals(CENTER) ? R.style.CenterDialogStyle : R.style.BottomDialogStyle;
        setStyle(DialogFragment.STYLE_NORMAL, dialogStyle);
    }

    @Override
    public void onStart() {
        super.onStart();
        mDialog = getDialog();
        if (mDialog == null) return;
        if (mOnKeyListener != null) {
            setOnKeyListener(mOnKeyListener);
        }
        mDialog.setCanceledOnTouchOutside(mCancel);
        Window window = mDialog.getWindow();
        if (window == null) return;
        window.setLayout(getWidth(), getHeight());
        window.getAttributes().gravity = getShowTag().equals(CENTER) ? Gravity.CENTER : Gravity.BOTTOM;
        window.setBackgroundDrawableResource(R.color.transparent);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = getViewBinding();
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    protected abstract void initView();

    protected abstract V getViewBinding();

    protected abstract String getShowTag();

    protected int getWidth() {
        return ViewGroup.LayoutParams.MATCH_PARENT;
    }

    protected int getHeight() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    /**
     * 第一次show
     */
    public void show(FragmentManager fm) {
        show(fm, getShowTag());
    }

    public void show() {
        mDialog.setCanceledOnTouchOutside(mCancel);
        try {
            mDialog.show();
        } catch (Exception e) {
            Log.e("BaseFragmentDialog", "show: ",e );
        }
    }

    public void hide() {
        mDialog.hide();
    }

    public BaseFragmentDialog<V> setCanceledOnTouchOutside(boolean cancel) {
        this.mCancel = cancel;
        return this;
    }

    public boolean isShowing() {
        if (mDialog.getWindow() == null) {
            return false;
        }
        return mDialog.isShowing() && mDialog.getWindow().getDecorView().getVisibility() == View.VISIBLE;
    }

    private void setOnKeyListener(DialogInterface.OnKeyListener onKeyListener) {
        mDialog.setOnKeyListener(onKeyListener);
    }
}
